package com.we823.cms.controller.manage.ex;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.controller.manage.AbstractManageController;
import com.we823.cms.model.extension.ProductCategory;
import com.we823.cms.repository.service.extension.ProductCategoryService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL+"/product_category")
public class ProductCategoryManageController extends AbstractManageController<ProductCategory, Integer> {

	private String baseUrl = WebConstants.MANAGE_URL+"/product_category/";
	
	private ProductCategoryService productCategoryService;
	
	@Autowired
	public ProductCategoryManageController(ProductCategoryService productCategoryService) {
		super(productCategoryService);
		super.baseUrl = baseUrl;
		this.productCategoryService=productCategoryService;
		super.entityName="productCategory";
	}

	@Override
	public PageInfo<ProductCategory> getData(PageRequest pageRequest,
											 HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<ProductCategory> productCategoryPage ;
		int parentid = RequestUtil.getValueByName(request, "parentid", -1);
		if(parentid==-1) {
			if (pageable) {
				productCategoryPage = productCategoryService.findAll(pageRequest);
			}else {
				productCategoryPage = new PageInfo<>(productCategoryService.findAll(getSort(request)));
			}
		}else {
			if (pageable) {
				productCategoryPage = productCategoryService.findByParentid(parentid,pageRequest);
			}else {
				productCategoryPage = new PageInfo<>(productCategoryService.findByParentid(parentid,getSort(request)));
			}
		}
		return productCategoryPage;
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("ex:product_category");
	}

}
